//
//  WaterDetailViewController.m
//  IphoneMapSdkDemo
//
//  Created by huck on 2017/5/22.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "WaterDetailViewController.h"
#import "AppUtils.h"
#import "HomeManager.h"
#import "MyTabView.h"
#import "WaterRecordView.h"
#import "WaterInfoView.h"

@interface WaterDetailViewController ()
@property (nonatomic, strong) MyTabView *tabView;
@property (nonatomic) NSInteger currentIndex; //当前搜索类型

@property (nonatomic) BOOL infoLoading; //基础信息加载中标记
@property (nonatomic) BOOL searchLoading; //查询加载中标记

@property (nonatomic ,strong)WaterInfoView *infoView;
@property (nonatomic ,strong)WaterRecordView *recordView;




@end

@implementation WaterDetailViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubView];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_infoView mapViewWillAppear];
    [_recordView initData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_infoView mapViewWillDisappear];
    [_recordView resignResponder];
}

- (void)initSubView
{
    self.title = _waterName;
    _currentIndex = 0;
    [HomeManager sharedInstance].waterDetailInfo = nil;
 
    [self initTabbarView];
    [self initTabView];
}

//tab初始化
-(void)initTabbarView
{
    NSArray *names = @[@"基础信息",@"巡查记录"];
    NSInteger index = 0;
    _tabView = [MyTabView tabViewWithFrameView:_tabbarFrame];
    [_tabView setTabTitles:names font:[UIFont systemFontOfSize:14] selColor:[UIColor colorWithString:COLOR_17A4FF] unselColor:[UIColor colorWithString:COLOR_989AA6]];
    [_tabView setSelectionChangeHandler:self action:@selector(onSelectTypeClick:)];
    [_tabView setIndicatorColor:[UIColor colorWithString:COLOR_17A4FF]];
    [_tabView selectIndex:index];
    [_tabView updateFrameWidth:SCREEN_WIDTH];
}

//tab切换
- (void)onSelectTypeClick:(NSNumber*)number
{
    if (nil == number) {
        return;
    }
    NSInteger index = [number integerValue];
    if(_currentIndex != index){
        _currentIndex = index;
            _infoViewContainer.hidden = _currentIndex!=0;
            _recordViewContainer.hidden = _currentIndex==0;
        [_recordView resignResponder];
    }
}

//初始化2个tab页面
-(void)initTabView
{
    _infoView = [WaterInfoView initViewWithFrameView:_infoViewContainer];
    _recordView = [WaterRecordView initViewWithFrameView:_recordViewContainer];
    
    if(![AppUtils hasConnected:YES]){
        return;
    }
    _infoLoading = YES;
    [Toast loading];
    [[HomeManager sharedInstance] requestWaterDetail:_waterId complete:^(BOOL success) {
        [_infoView updateInfo];
        _infoLoading = NO;
        if(!_infoLoading && !_searchLoading){
            [Toast loaded];
        }
    }];
    _recordView.waterId = _waterId;
//    [_recordView initData];
}




@end
